using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Palaso.Reporting;
using SpellCenter.Core;

namespace SpellCenter.UI
{
    internal class SpellGridViewControlPM
    {
        private CorpusIndex _index;
        private IList<SpellWordInfo> _data;
        private FilesInCorpusList _config;
        private readonly IWordContext _context;

        public SpellGridViewControlPM(IWordContext context)
        {
            _data = new List<SpellWordInfo>();
            _context = context;
        }

        /// <summary>
        /// Load spell words from a CorpusConfig object
        /// </summary>
        /// <param name="config"></param>
        /// <param name="indexer"></param>
        public void OpenConfigFile(FilesInCorpusList config, IIndexingManager indexer)
        {
            try
            {
                _config = config;
                _index = indexer.BeginIndexing(_config);
            }
            catch (ArgumentException e)
            {
//                indexer.SetState(JobStatusState.Finished);
#if DEBUG
                ErrorNotificationDialog.ReportException(e, null, false);
#endif
            }
        }

        public void OpenConfigFile(string filename, IIndexingManager indexer)
        {
            OpenConfigFile(FilesInCorpusList.CreateFromFile(new FileInfo(filename)), indexer);
        }

        public FilesInCorpusList Config
        {
            get { return _config; }
        }

        public IList<SpellWordInfo> Data
        {
            get { return _data; }
            set{ _data = value; }
        }

        public void ViewWordContext(int index)
        {
            if(index >= 0 && index < _data.Count)
            {
                _context.UseWord(index);
            }
        }

        private Control _form;
//        private WritingSystemDictionary _dictionary;

        public Control ParentForm
        {
            get { return _form; }
            set
            {
                _form = value;
            }
        }

      public CorpusIndex CorpusIndex
      {
        get { return this._index; }
      }

      public void FillDataWithSpellWords()
        {
            _data = new List<SpellWordInfo>(this.CorpusIndex.GetWords());
            _context.SetWords(_data);
        }

        public void ImportDictionary(string filename)
        {
            //CreateDictionaryIfNull(_config.WritingSystemId);
            WritingSystemDictionary.AddGoodWordsFromFile(_config.WritingSystemId, filename);
        }

        //private void CreateDictionaryIfNull(string name)
        //{
        //    if(_dictionary == null)
        //    {
        //        FileInfo forFormatting = new FileInfo(name);
        //        string ext = forFormatting.Extension;
        //        string writingSystem = forFormatting.Name.Substring(0, forFormatting.Name.Length - ext.Length);
        //        _dictionary = new WritingSystemDictionary(writingSystem);
        //    }
        //}

        public void ExportDictionary(string fileName)
        {
//            CreateDictionaryIfNull(_config.WritingSystemId);
            //if (_index != null)
            //{
            //    _index.ExportDictionary(_dictionary, fileName);
            //}
          //Todo: move ExportDictionary to correct spot
        }

        public void ChangeSpelling(int row, string newSpelling, ISpellingChangeManager manager)
        {
            manager.Finished += delegate { _data = new List<SpellWordInfo>(this.CorpusIndex.GetWords()); };
            manager.ChangeSpelling(this.CorpusIndex, _data[row].Word, newSpelling);
        }

        public void UpdateCorpus()
        {
            if (this.CorpusIndex != null)
            {
                this.CorpusIndex.Update();
            }
        }

        public bool IsCorpusUpToDate()
        {
            if(this.CorpusIndex != null && !this.CorpusIndex.IsUpToDate())
                return false;
            return true;
        }
    }
}